﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using MvcSiteMapProvider.Extensibility;
using System.Web.Routing;

namespace MvcSiteMapProvider
{
    /// <summary>
    /// Default SiteMapNode URL resolver.
    /// </summary>
    public class DefaultSiteMapNodeUrlResolver
        : ISiteMapNodeUrlResolver
    {
        #region ISiteMapNodeUrlResolver Members

        /// <summary>
        /// Resolves the URL.
        /// </summary>
        /// <param name="mvcSiteMapNode">The MVC site map node.</param>
        /// <param name="area">The area.</param>
        /// <param name="controller">The controller.</param>
        /// <param name="action">The action.</param>
        /// <param name="routeValues">The route values.</param>
        /// <returns>The resolved URL.</returns>
        public virtual string ResolveUrl(MvcSiteMapNode mvcSiteMapNode, string area, string controller, string action, IDictionary<string, object> routeValues)
        {
            RequestContext ctx;
            if (HttpContext.Current.Handler is MvcHandler)
                ctx = ((MvcHandler)HttpContext.Current.Handler).RequestContext;
            else
                ctx = new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData());

            return new UrlHelper(ctx).Action(action, controller, new RouteValueDictionary(routeValues));
        }

        #endregion
    }
}
